package _pinyin;

import com.alibaba.fastjson.JSON;
import com.hankcs.hanlp.dictionary.py.Pinyin;
import com.hankcs.hanlp.dictionary.py.PinyinDictionary;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author huangya
 * @date 2023/8/14
 */
public class Pinyin4jDemo {
    public static Map<String, String> changeChinesePinyin(String chinese) throws BadHanyuPinyinOutputFormatCombination {
        Map<String, String> pinyin = new HashMap<String, String>();

        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);

        StringBuffer fullPinyin = new StringBuffer();
        StringBuffer simplePinyin = new StringBuffer();
        StringBuffer firstPinyin = new StringBuffer();

        char[] chineseChar = chinese.toCharArray();
        for (int i = 0; i < chineseChar.length; i++) {
            String[] str = null;
            try {
                str = PinyinHelper.toHanyuPinyinStringArray(chineseChar[i],
                        format);
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                e.printStackTrace();
            }
            if (str != null) {
                if (str.length == 0) {
                    continue;
                }
                fullPinyin = fullPinyin.append(str[0].toString());
                simplePinyin = simplePinyin.append(str[0].charAt(0));

            }
            if (str == null) {
                String regex = "^[0-9]*[a-zA-Z]*+$";
                Pattern pattern = Pattern.compile(regex);
                Matcher m = pattern.matcher(String.valueOf(chineseChar[i]));
                if (m.find()) {
                    fullPinyin = fullPinyin.append(chineseChar[i]);
                    simplePinyin = simplePinyin.append(chineseChar[i]);
                }
            }
        }
        String[] name = PinyinHelper.toHanyuPinyinStringArray(chineseChar[0], format);
        if(name == null && fullPinyin.substring(0,1).matches("[a-zA-Z]")){
            name = new String[]{fullPinyin.substring(0,1)};
        }else if (name == null){
            name = new String[]{"#"};
        }
        firstPinyin = firstPinyin.append(name[0].charAt(0));
        pinyin.put("fullPinyin", fullPinyin.toString());
        pinyin.put("simplePinyin", simplePinyin.toString().toUpperCase());
        pinyin.put("groupPinyin", firstPinyin.toString().toUpperCase());
        return pinyin;
    }

    public static void main(String[] args) throws BadHanyuPinyinOutputFormatCombination {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 100, 5,
                TimeUnit.MINUTES, new ArrayBlockingQueue<>(1024), new ThreadPoolExecutor.DiscardPolicy());

        for (int j = 0; j < 1000; j++) {

            int size = 1000000;

            CompletableFuture<String>[] completableFutures = new CompletableFuture[size];
            for (int i = 0; i < size; i++) {
                completableFutures[i] = CompletableFuture.supplyAsync(() -> {
                    try {
                        TimeUnit.MILLISECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    return "1";
                }, threadPoolExecutor);
            }

            for (int i = 0; i < size; i++) {
                try {
                    completableFutures[i].get();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } catch (ExecutionException e) {
                    throw new RuntimeException(e);
                }
            }

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }


//        Map<String, String> fydmcPinYinMap = changeChinesePinyin("拼音");
//        System.out.println(fydmcPinYinMap.get("fullPinyin"));
//        String simplePinyin = fydmcPinYinMap.get("simplePinyin");
//        System.out.println(simplePinyin);
//        System.out.println(fydmcPinYinMap.get("groupPinyin"));
//        System.out.println(simplePinyin+":最终");


//        //pinyin4j
//        int pinyin4jSize=0;
//        for (int j = 0; j< 10; j++) {
//            long start = System.currentTimeMillis();
//            for (int i = 19968; i <= 40959; i++) {
//                char word = (char) i;
//                String[] pinyin = PinyinHelper.toHanyuPinyinStringArray(word);
//                pinyin4jSize += pinyin==null ? 0 : pinyin.length;
//            }
//            long end = System.currentTimeMillis();
//            System.out.println(end- start);
////            System.out.println("----------------------" + pinyin4jSize + "----------------------");
//        }
//
//        //hanlp
//        int hanlpSize=0;
//        for (int j = 0; j< 10; j++) {
//            long start = System.currentTimeMillis();
//            for (int i = 19968; i <= 40959; i++) {
//                char word = (char) i;
//                List<Pinyin> pinyins = PinyinDictionary.convertToPinyin(String.valueOf(word));
//                hanlpSize += pinyins==null ? 0 : pinyins.size();
//            }
//            long end = System.currentTimeMillis();
//            System.out.println(end- start);
////            System.out.println("----------------------" + hanlpSize + "----------------------");
//        }
//
//        //jpinyin
//        int jpinyinSize=0;
//        for (int j = 0; j< 10; j++) {
//            long start = System.currentTimeMillis();
//            for (int i = 19968; i <= 40959; i++) {
//                char word = (char) i;
//                String[] pinyin = com.github.stuxuhai.jpinyin.PinyinHelper.convertToPinyinArray(word);
//                jpinyinSize += pinyin==null ? 0 : pinyin.length;
//            }
//            long end = System.currentTimeMillis();
//            System.out.println(end- start);
////            System.out.println("----------------------" + jpinyinSize + "----------------------");
//        }

    }


}


